﻿/*
 * Utilisateur: Masy Freddy
 * Date: 31/10/2011
 * Heure: 12:27
 */

namespace ReaderDvdLibrary
{
	using System.IO;
	using System.Xml.Serialization;
	using MetaVideoEditorEvolved.AddIn.AddInViews;
	using MetaVideoEditorEvolved.AddIn.Entity;
	using MetaVideoEditorEvolved.Helper;
	using System;
	using System.Collections.Generic;
	using System.Linq;
	
	[System.AddIn.AddIn("Dvd Library Reader",
	                    Version = "1.0.0.0",
	                    Description = "Dvd Library Reader",
	                    Publisher = "Masy Freddy")]
	public class DvdLibrary : ReaderView
	{
		private ConfigurationAddIn configurationAddIn;
		
		public DvdLibrary()
		{
			configurationAddIn = ConfigurationManager.Deserialize();
			
			if(configurationAddIn == null)
			{
				configurationAddIn = new ConfigurationAddIn()
				{
					InfoCacheFolder = string.Empty,
					TraceLevel = "Error"
				};
				ConfigurationManager.Serialize(configurationAddIn);
			}
			
			Tracer.TracerInstance.Level= string.IsNullOrEmpty(configurationAddIn.TraceLevel) ? TracerLevel.Error : (TracerLevel)Enum.Parse(typeof(TracerLevel), configurationAddIn.TraceLevel);
			Tracer.TracerInstance.FileName = "DvdLibraryReader";
		}
		
		public override MetaVideoEditorEvolved.AddIn.Entity.Movie ReadMovie(string mediaLocation)
		{
			try
			{
				Tracer.TracerInstance.TraceInformation("Read Movie in location " + mediaLocation);
				
				if(!string.IsNullOrEmpty(configurationAddIn.InfoCacheFolder))
				{
					Disc disc = null;
					
					string metadataPath = Path.Combine(mediaLocation, "mvee.dvdid.xml");
					
					if(File.Exists(metadataPath))
					{
						using (StreamReader sr = new StreamReader(metadataPath))
						{
							disc = new XmlSerializer(typeof(Disc)).Deserialize(sr) as Disc;
						}
					}
					
					if(disc != null)
					{
						metadataPath = Path.Combine(configurationAddIn.InfoCacheFolder, disc.ID + ".xml");
						
						if(File.Exists(metadataPath))
						{
							METADATA metadata = null;
							
							using (StreamReader sr = new StreamReader(metadataPath))
							{
								metadata = new XmlSerializer(typeof(METADATA)).Deserialize(sr) as METADATA;
							}
							
							if(metadata != null)
							{
								var movie = new MetaVideoEditorEvolved.AddIn.Entity.Movie()
								{
									Countries = new List<string>(),
									Trailers = new List<Trailer>(),
									CastMembers = new List<CastMember>(),
								};
								
								if(metadata.MDRDVD != null)
								{
									movie.Title = metadata.MDRDVD.dvdTitle;
									movie.Studios = string.IsNullOrEmpty(metadata.MDRDVD.studio) ? new List<string>() : metadata.MDRDVD.studio.Split(';').ToList();
									movie.Genres = string.IsNullOrEmpty(metadata.MDRDVD.genre) ? new List<string>() : metadata.MDRDVD.genre.Split(';').ToList();
									movie.MPAARating = metadata.MDRDVD.MPAARating;
									movie.Duration = string.IsNullOrEmpty(metadata.MDRDVD.duration) ? 0 : int.Parse(metadata.MDRDVD.duration);
									
									if(metadata.MDRDVD.title != null)
									{
										movie.Overview = metadata.MDRDVD.title.synopsis;
									}
									
									if(!string.IsNullOrEmpty(metadata.MDRDVD.leadPerformer))
									{
										foreach (var element in metadata.MDRDVD.leadPerformer.Split(';')) {
											movie.CastMembers.Add(new CastMember
											                      {
											                      	FullName = element,
											                      	Activity = "Actor"
											                      });
										}
									}
									
									if(!string.IsNullOrEmpty(metadata.MDRDVD.director))
									{
										foreach (var element in metadata.MDRDVD.director.Split(';')) {
											movie.CastMembers.Add(new CastMember
											                      {
											                      	FullName = element,
											                      	Activity = "Director"
											                      });
										}
									}
									
									
									movie.Backdrops = (from b in new DirectoryInfo(mediaLocation).GetFiles("backdrop*")
									                   orderby b.FullName
									                   select new Uri(b.FullName)).ToList();
									
									movie.Posters = File.Exists(Path.Combine(mediaLocation, "folder.jpg")) ?
										new List<Uri>()
									{
										new Uri(Path.Combine(mediaLocation, "folder.jpg"))
									} : new List<Uri>();
									
									return movie;
								}
							}
						}
					}
				}
				
				return null;
			}
			catch(Exception ex)
			{
				Tracer.TracerInstance.TraceError("Exception in the reader dvd library", ex);
				throw;
			}
		}
		
		public override MetaVideoEditorEvolved.AddIn.Entity.Episode ReadEpisode(string mediaLocation)
		{
			return null;
		}
		
		public override MetaVideoEditorEvolved.AddIn.Entity.Season ReadSeason(string mediaLocation)
		{
			return null;
		}
		
		public override MetaVideoEditorEvolved.AddIn.Entity.Serie ReadSerie(string mediaLocation)
		{
			return null;
		}
		
		public override bool HasOption()
		{
			return true;
		}
		
		public override void ShowOption()
		{
			new Option(configurationAddIn).ShowDialog();
		}
	}
}